package com.frog.framework.rekognition.factory;

import com.frog.framework.rekognition.config.RekognitionConfig;
import org.springframework.util.Assert;
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.rekognition.RekognitionClient;
import software.amazon.awssdk.services.rekognition.RekognitionClientBuilder;
import software.amazon.awssdk.utils.StringUtils;

import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author wuqiong 2022/11/7
 * @description
 */
public class RekognitionEndpointFactory {
    private static final Map<String, RekognitionClient> endpointMap = new ConcurrentHashMap<>();
    private static final String defaultStr = "defaults";

    /**
     * get Rekognition Client  endpoint
     *
     * @param clientName
     * @return RekognitionClient
     * @author wuqiong 2022/11/7 18:03
     */
    public static RekognitionClient getRekognitionClient(String clientName) {
        if (StringUtils.isBlank(clientName)) clientName = defaultStr;
        return endpointMap.get(clientName);
    }

    /**
     * create batch Rekognition Client
     *
     * @param rekognitionConfig
     * @author wuqiong 2022/11/7 18:52
     */
    public static void createBatchByConfig(RekognitionConfig rekognitionConfig) {
        Assert.isTrue(!rekognitionConfig.getInstances().isEmpty(), "Rekognition config 为空,请检查");
        if (!rekognitionConfig.getInstances().containsKey(defaultStr)) {
            Optional<String> first = rekognitionConfig.getInstances().keySet().stream().findFirst();
            RekognitionClient client = createRekognitionClient(first.get(), rekognitionConfig.getInstances().get(first.get()));
            endpointMap.put(first.get(), client);
            endpointMap.put(defaultStr, client);
        }
        rekognitionConfig.getInstances().forEach((k, v) -> endpointMap.put(k, createRekognitionClient(k, v)));
    }

    /**
     * create Rekognition Client by config endpointInstance
     *
     * @return RekognitionClient
     * @author wuqiong 2022/11/7 18:52
     */
    public static synchronized RekognitionClient createRekognitionClient(String clientName, RekognitionConfig.EndpointInstance endpointInstance) {
        Assert.isTrue(endpointInstance != null, "Rekognition endpointInstance is null , please check ");
        if (endpointMap.containsKey(clientName)) return endpointMap.get(clientName);
        RekognitionClientBuilder builder = RekognitionClient.builder();
        builder.region(Region.of(endpointInstance.getRegion()));
        if (StringUtils.isNotBlank(endpointInstance.getAccessKeyId()) && StringUtils.isNotBlank(endpointInstance.getSecretAccessKey())) {
            builder.credentialsProvider(StaticCredentialsProvider.create(
                    AwsBasicCredentials.create(endpointInstance.getAccessKeyId(), endpointInstance.getSecretAccessKey())));
        }
        return builder.build();
    }
}
